<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 二维码类
 * @package  module/base/common
 * @author   allen.qiang
 * @version 1.0
 */

quanli_require('framework/qrcode/qrlib.php');

class quanliQrcode {

    public function create( $text, $fileName = '', $pointSize = 12, $level = 'H' ) {

        //二维码内容
        if (!isset($text) || empty($text)) {
            return array( 'code' => 400 , 'message' => '二维码内容不能为空');
        }

        //二维码的质量
        if (!in_array($level , array('L', 'M', 'Q', 'H')) ){
            return array( 'code' => 400 , 'message' => '二维码质量参数错误' );
        }

        //二维码的黑点像素
        if (!is_numeric($pointSize) || $pointSize <= 0) {
            return array( 'code' => 400 , 'message' => '二维码黑点像素参数错误' );
        }

        //检查二维码目录
        $dirname = G_IMAGE_SAVE_ROOT . 'qrcode/';
        if (!file_exists($dirname)) {
            mkdir($dirname, 0777, true );
        }

        //二维码路径
        if (empty($fileName)) {
            $fileName = $text;
        }
        $filePath = $dirname . $fileName . '.png';

        //生成二维码
        QRcode::png($text , $filePath, $level, $pointSize , 2);

        return array('code' => 200 , 'message' => 'ok');

    }

}
